/**
 * Created by yangyan on 18/2/1.
 */


function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }

    // 当前的年月日
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate;

    // 当前的年月日时分秒
    // var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
    //     + " " + date.getHours() + seperator2 + date.getMinutes()
    //     + seperator2 + date.getSeconds();
    return currentdate;
}


// 三级选择-------注省市区选择
function adresssCity() {
    var selectContactDom = $('#select_contact');
    var showContactDom = $('#show_contact');
    var contactProvinceCodeDom = $('#contact_province_code');
    var contactCityCodeDom = $('#contact_city_code');
    selectContactDom.bind('click', function () {
        var oneLevelId = showContactDom.attr('data-province-code');
        var twoLevelId = showContactDom.attr('data-city-code');
        var threeLevelId = showContactDom.attr('data-district-code');
        var iosSelect = new IosSelect(3,
            [iosProvinces, iosCitys, iosCountys],
            {
                title: '请选择区域',
                itemHeight: 35,
                relation: [1, 1],
                oneLevelId: oneLevelId,
                twoLevelId: twoLevelId,
                threeLevelId: threeLevelId,
                callback: function (selectOneObj, selectTwoObj, selectThreeObj) {
                    contactProvinceCodeDom.val(selectOneObj.id);
                    contactProvinceCodeDom.attr('data-province-name', selectOneObj.value);
                    contactCityCodeDom.val(selectTwoObj.id);
                    contactCityCodeDom.attr('data-city-name', selectTwoObj.value);

                    showContactDom.attr('data-province-code', selectOneObj.id);
                    showContactDom.attr('data-city-code', selectTwoObj.id);
                    showContactDom.attr('data-district-code', selectThreeObj.id);
                    showContactDom.html(selectOneObj.value + ',' + selectTwoObj.value + ',' + selectThreeObj.value);
                    var  adress = document.getElementById("show_contact").innerHTML;
                    if (adress=="请选择您的上门取件地址（省市区）"){
                    }else {
                        changeStyle2();
                    }
                }
            });
    });

}

function changeStyle2() {
    var obj = document.getElementById("show_contact");
    obj.style.cssText = "color:#1b1b1b;";
}



// 三列选择------注日期选择


function datatime() {
    var selectDateDom = $('#showDateClick');
    var showDateDom = $('#showDate');
// 初始化时间
    var now = new Date();
    var nowYear = now.getFullYear();
    var nowMonth = now.getMonth() + 1;
    var nowDate = now.getDate();
    showDateDom.attr('data-year', nowYear);
    showDateDom.attr('data-month', nowMonth);
    showDateDom.attr('data-date', nowDate);

// 数据初始化
    function formatYear(nowYear) {
        var arr = [];
        for (var i = nowYear - 5; i <= nowYear + 5; i++) {
            arr.push({
                id: i + '',
                value: i + '年'
            });
        }
        return arr;
    }
    function formatMonth() {
        var arr = [];
        for (var i = 1; i <= 12; i++) {
            arr.push({
                id: i + '',
                value: i + '月'
            });
        }
        return arr;
    }
    function formatDate(count) {
        var arr = [];
        for (var i = 1; i <= count; i++) {
            arr.push({
                id: i + '',
                value: i + '日'
            });
        }
        return arr;
    }
    var yearData = function (callback) {
        // settimeout只是模拟异步请求，真实情况可以去掉
        // setTimeout(function() {
        callback(formatYear(nowYear))
        // }, 2000)
    }
    var monthData = function (year, callback) {
        // settimeout只是模拟异步请求，真实情况可以去掉
        // setTimeout(function() {
        callback(formatMonth());
        // }, 2000);
    };
    var dateData = function (year, month, callback) {
        // settimeout只是模拟异步请求，真实情况可以去掉
        // setTimeout(function() {
        if (/^(1|3|5|7|8|10|12)$/.test(month)) {
            callback(formatDate(31));
        }
        else if (/^(4|6|9|11)$/.test(month)) {
            callback(formatDate(30));
        }
        else if (/^2$/.test(month)) {
            if (year % 4 === 0 && year % 100 !== 0 || year % 400 === 0) {
                callback(formatDate(29));
            }
            else {
                callback(formatDate(28));
            }
        }
        else {
            throw new Error('month is illegal');
        }
        // }, 2000);
        // ajax请求可以这样写
        /*
         $.ajax({
         type: 'get',
         url: '/example',
         success: function(data) {
         callback(data);
         }
         });
         */
    };
    selectDateDom.bind('click', function () {
        var oneLevelId = showDateDom.attr('data-year');
        var twoLevelId = showDateDom.attr('data-month');
        var threeLevelId = showDateDom.attr('data-date');

        var iosSelect = new IosSelect(3,
            [yearData, monthData, dateData],
            {
                title: '请选择日期',
                itemHeight: 35,
                oneLevelId: oneLevelId,
                twoLevelId: twoLevelId,
                threeLevelId: threeLevelId,
                showLoading: true,
                callback: function (selectOneObj, selectTwoObj, selectThreeObj) {
                    if (selectTwoObj.id<10){
                        selectTwoObj.id="0"+selectTwoObj.id;
                    }
                    if (selectThreeObj.id<10){
                        selectThreeObj.id="0"+selectThreeObj.id;
                    }

                    showDateDom.attr('data-year', selectOneObj.id);
                    showDateDom.attr('data-month', selectTwoObj.id);
                    showDateDom.attr('data-date', selectThreeObj.id);
                    // 添加0之后的年月日
                    showDateDom.html(selectOneObj.id+'年' + selectTwoObj.id +'月' +  selectThreeObj.id +'日');
                    // 正常显示的年月日
                    // showDateDom.html(selectOneObj.value + selectTwoObj.value + selectThreeObj.value);


                    var  dataStr = document.getElementById("showDate").innerHTML;

                    if (dataStr=="选择开始日期）"){
                    }else {
                        DatachangeStyle2();
                    }
                }
            });
    });

}




function entdatatime() {
    var selectDateDom = $('#endADateClick');
    var endADateDom = $('#endADate');
// 初始化时间
    var now = new Date();
    var nowYear = now.getFullYear();
    var nowMonth = now.getMonth() + 1;
    var nowDate = now.getDate();


    endADateDom.attr('data-year', nowYear);
    endADateDom.attr('data-month', nowMonth);
    endADateDom.attr('data-date', nowDate);
// 数据初始化
    function formatYear(nowYear) {
        var arr = [];
        for (var i = nowYear - 5; i <= nowYear + 5; i++) {
            arr.push({
                id: i + '',
                value: i + '年'
            });
        }
        return arr;
    }
    function formatMonth() {
        var arr = [];
        for (var i = 1; i <= 12; i++) {
            arr.push({
                id: i + '',
                value: i + '月'
            });
        }
        return arr;
    }
    function formatDate(count) {
        var arr = [];
        for (var i = 1; i <= count; i++) {
            arr.push({
                id: i + '',
                value: i + '日'
            });
        }
        return arr;
    }
    var yearData = function (callback) {
        // settimeout只是模拟异步请求，真实情况可以去掉
        // setTimeout(function() {
        callback(formatYear(nowYear))
        // }, 2000)
    }
    var monthData = function (year, callback) {
        // settimeout只是模拟异步请求，真实情况可以去掉
        // setTimeout(function() {
        callback(formatMonth());
        // }, 2000);
    };
    var dateData = function (year, month, callback) {
        // settimeout只是模拟异步请求，真实情况可以去掉
        // setTimeout(function() {
        if (/^(1|3|5|7|8|10|12)$/.test(month)) {
            callback(formatDate(31));
        }
        else if (/^(4|6|9|11)$/.test(month)) {
            callback(formatDate(30));
        }
        else if (/^2$/.test(month)) {
            if (year % 4 === 0 && year % 100 !== 0 || year % 400 === 0) {
                callback(formatDate(29));
            }
            else {
                callback(formatDate(28));
            }
        }
        else {
            throw new Error('month is illegal');
        }
        // }, 2000);
        // ajax请求可以这样写
        /*
         $.ajax({
         type: 'get',
         url: '/example',
         success: function(data) {
         callback(data);
         }
         });
         */
    };
    selectDateDom.bind('click', function () {

        var oneLevelId = endADateDom.attr('data-year');
        var twoLevelId = endADateDom.attr('data-month');
        var threeLevelId = endADateDom.attr('data-date');

        var iosSelect = new IosSelect(3,
            [yearData, monthData, dateData],
            {
                title: '请选择日期',
                itemHeight: 35,
                oneLevelId: oneLevelId,
                twoLevelId: twoLevelId,
                threeLevelId: threeLevelId,
                showLoading: true,
                callback: function (selectOneObj, selectTwoObj, selectThreeObj) {

                    if (selectTwoObj.id<10){
                        selectTwoObj.id="0"+selectTwoObj.id;
                    }
                    if (selectThreeObj.id<10){
                        selectThreeObj.id="0"+selectThreeObj.id;
                    }

                    endADateDom.attr('data-year', selectOneObj.id);
                    endADateDom.attr('data-month', selectTwoObj.id);
                    endADateDom.attr('data-date', selectThreeObj.id);

                    endADateDom.html(selectOneObj.id+'年' + selectTwoObj.id +'月' +  selectThreeObj.id +'日');

                    var  endADateStr = document.getElementById("endADate").innerHTML;
                    if (endADateStr=="选择结束日期）"){
                    }else {
                        DatachangeStyle3();
                    }
                }
            });
    });

}

function DatachangeStyle2() {
    var obj = document.getElementById("showDate");
    obj.style.cssText = "color:#1b1b1b;";
}


function DatachangeStyle3() {
    var  endADateStr = document.getElementById("endADate");
    endADateStr.style.cssText = "color:#1b1b1b;";
}


// 单个仿ios滑轮页面-------注时间段选择
function timeDemo() {
    var showBankDom = document.querySelector('#demo55');
    var showBankDom2 = document.querySelector('#sell');
    var bankIdDom = document.querySelector('#timeStr');
    showBankDom.addEventListener('click', function () {
        var bankId = showBankDom.dataset['id'];
        var bankName = showBankDom.dataset['value'];
        var bankSelect = new IosSelect(1,
            [data],
            {
                container: '.container',
                title: '请选择预约时间段',
                itemHeight: 50,
                itemShowCount: 3,
                oneLevelId: bankId,
                callback: function (selectOneObj) {
                    bankIdDom.value = selectOneObj.id;
                    showBankDom2.innerHTML = selectOneObj.value;
                    showBankDom.dataset['id'] = selectOneObj.id;
                    showBankDom.dataset['value'] = selectOneObj.value;
                    var  sell = document.getElementById("sell").innerHTML;
                    if (sell=="请选择预约时间段）"){
                    }else {
                        TimechangeStyle2();
                    }
                }
            });
    });
}


function TimechangeStyle2() {
    var obj = document.getElementById("sell");
    obj.style.cssText = "color:#1b1b1b;";
}
